﻿@namespace Oqtane.Modules.Admin.Profiles
@inherits ModuleBase
@inject IProfileService ProfileService

@if (_profiles == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <ActionLink Action="Add" Security="SecurityAccessLevel.Admin" Text="Add Profile" />

    <Pager Items="@_profiles">
        <Header>
            <th style="width: 1px;">&nbsp;</th>
            <th style="width: 1px;">&nbsp;</th>
            <th>Name</th>
        </Header>
        <Row>
            <td><ActionLink Action="Edit" Parameters="@($"id=" + context.ProfileId.ToString())" /></td>
            <td><ActionDialog Header="Delete Profile" Message="@("Are You Sure You Wish To Delete " + context.Name + "?")" Action="Delete" Class="btn btn-danger" OnClick="@(async () => await DeleteProfile(context.ProfileId))" /></td>
            <td>@context.Name</td>
        </Row>
    </Pager>
}

@code {
    private List<Profile> _profiles;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;

    protected override async Task OnInitializedAsync()
    {
        _profiles = await ProfileService.GetProfilesAsync(PageState.Site.SiteId);
    }

    private async Task DeleteProfile(int profileId)
    {
        try
        {
            await ProfileService.DeleteProfileAsync(profileId);
            await logger.LogInformation("Profile Deleted {ProfileId}", profileId);
            AddModuleMessage("Profile Deleted", MessageType.Success);
        }
        catch (Exception ex)
        {
            await logger.LogError(ex, "Error Deleting Profile {ProfileId} {Error}", profileId, ex.Message);
            AddModuleMessage("Error Deleting Profile", MessageType.Error);
        }
    }
}
